package com.sxt.business.controller;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.business.domain.Goods;
import com.sxt.business.service.IGoodsService;
import com.sxt.business.vo.GoodsVO;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.utils.ResultObj;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-15
 */
@RestController
@RequestMapping("goods")
public class GoodsController {

	@Autowired
	private IGoodsService goodsService;
	
	private Log log = LogFactory.getLog(GoodsController.class);
	
	/**
	 * 查询
	 */
	@RequestMapping("loadAllGoods")
	public DataGridView loadAllGoods(GoodsVO goodsVO) {
		return this.goodsService.queryAllGoods(goodsVO);
	}
	
	/**
	 * 添加
	 */
	@RequestMapping("addGoods")
	public Object addGoods(Goods goods) {
		try {
			this.goodsService.addGoods(goods);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			log.error("添加错误");
			return ResultObj.ADD_ERROR;
		}
	}
	
	/**
	 * 	修改
	 */
	@RequestMapping("updateGoods")
	public ResultObj updateGoods(Goods goods) {
		try {
			this.goodsService.updateGoods(goods);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			log.error("修改失败");
			return ResultObj.UPDATE_ERROR;
		}
	}
	
	/**
	 * 	批量删除
	 */
	@RequestMapping("batchDeleteGoods")
	public ResultObj batchDeleteGoods(Integer [] ids) {
		try {
			if(ids==null||ids.length==0) {
				log.error("参数不能为空");
				return ResultObj.DELETE_ERROR;
			}
			//Collection<Serializable> idList = new ArrayList<Serializable>();
			for (Integer  integer  : ids) {
				//idList.add(integer);
				this.goodsService.removeById(integer);
			}
			//this.goodsService.removeByIds(idList);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			log.error("删除失败");
			return ResultObj.DELETE_ERROR;
		}
	}
	
	/**
	 * 	删除
	 */
	@RequestMapping("deleteGoods")
	public ResultObj deleteGoods(Integer id) {
		try {
			goodsService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			log.error("删除失败");
			return ResultObj.DELETE_ERROR;
		}
	}
	
	/**
	 * 加载可以商品的不分页
	 */
	@RequestMapping("loadAllAvailableGoods")
	public DataGridView loadAllAvailableGoods() {
		return this.goodsService.queryAllAvailableGoods();
	}
	
	/**
	 * 根据供货商ID查询商品
	 */
	@RequestMapping("loadGoodsByProviderid")
	public DataGridView loadGoodsByProviderid(Integer providerid) {
		return this.goodsService.loadGoodsByProviderid(providerid);
	}
}

